<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="com.googlecode.contests_repo.core.config.ConfigurationManager"%>
<%@page import="com.googlecode.contests_repo.core.WebTools"%>
<%@page import="com.googlecode.contests_repo.core.WebTools.ResourceType"%>
<%@page import="com.googlecode.contests_repo.core.users.*"%>
<%@page import="com.googlecode.contests_repo.core.model.ResourcesUtils"%>
<%@page import="java.net.Authenticator.RequestorType"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.io.File"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Contest Repo - Resource</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="assets/style/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
	<%@include file="include/header.jsp"%>
	<%
		String path = request.getParameter("path"); // relative path 
		boolean canEdit = "1".equals(request.getParameter("edit"));
		String editParameter = canEdit ? "&amp;edit=1" : "";
		String seriesRelativePath = ConfigurationManager.getInstance()
				.getSeriesRelativePath();
		RequestDispatcher dispatcher;

		if (path == null) {
			path = seriesRelativePath;
			response.sendRedirect("resource.jsp?path=" + seriesRelativePath);
		}
		if (request.getParameter("validate") != null) {
			out.flush();
			dispatcher = request
					.getRequestDispatcher("/ValidateResourceServlet");
			dispatcher.include(request, response);
			out.flush();
		}
		if (request.getParameter("commit") != null) {
			String result = WebTools.createEmptyResource(path,
					request.getParameter("new-resource"));
			if ("".equals(result)) {
				out.println("<div style='color:green'>Resource created successfully</div>");
			} else {
				out.println(String.format(
						"<div style='color:red'>%s</div>", result));
			}
		}
		ResourceType resourceType = WebTools.getResourceType(path);
		String handler = WebTools.getResourceHandler(path);
		out.flush();
		if (handler != null) {
			dispatcher = request.getRequestDispatcher(handler);
			dispatcher.include(request, response);
		} else {
			response.sendRedirect(String.format("resource.jsp?path=%s",
					seriesRelativePath));
		}
		out.flush();

		// Edit link
		String userName = (String) session.getAttribute("username");
		String repoRoot = ConfigurationManager.getInstance().getRepoRoot();
		String resourcePath = repoRoot + File.separator + path;
		boolean hasUserEditRights = UserManager.hasAccessRight(userName, resourcePath, AccessRight.VIEW_FULL);
		if (hasUserEditRights && resourceType != ResourceType.SERIES_LIST
				&& resourceType != ResourceType.ADDITIONAL_FILE) {
	%>
	<form method="post" action="">
		<p><input type="submit" name="validate" value="Validate resource" /></p>
	</form>
	<% } %>
	<p>
	<% 
		if (!canEdit && hasUserEditRights) {
	%>
	<a href="resource.jsp?path=<%=path%>&amp;edit=1">Edit</a>
	<%
		}
		if (canEdit && hasUserEditRights) {
	%>
	<a href="resource.jsp?path=<%=path%>">Finish editing</a>
	<% } %>
	</p>
	<%
		if (resourceType != null) {
		// Create resource form
		ResourceType nextResource = resourceType.getSuccessor();
		if (canEdit && hasUserEditRights && nextResource != null) {
	%>
	<form method="post" action="">
		<p>
			<input type="text" name="new-resource" />
			<input type="submit" name="commit" value="Add <%=nextResource.getResourceTypeName()%>" />
		</p>
	</form>
	<%
		}
		if (canEdit && hasUserEditRights && resourceType.getResourceTypeName().equals("problem")) {
			%>
			<form method="post" action="">
				<p>
					<input type="text" name="new-resource" />
					<input type="submit" name="commit" value="Add Problem Resource" />
				</p>
			</form>
			<%
				}
		// Back button
		if (!seriesRelativePath.equals(path)) {
	%>
	<p><a href="resource.jsp?path=<%=WebTools.getRelativeParentPath(path)%>">Back</a></p>
	<%
		}
		if (resourceType != ResourceType.SERIES_LIST
				&& resourceType != ResourceType.ADDITIONAL_FILE) {
			boolean canView = false;
			if (hasUserEditRights) {
				canView = true;
			} else if (!UserManager.hasAccessRight(userName, resourcePath,
					AccessRight.VIEW)) {
				canView = false;
			}
			// contest and problem

			if (canView) {
	%>
	<p>
	<a href="resource.jsp?path=<%=path + File.separator + "_public_files" + editParameter%>">
			Public extra resources </a>
	<%
		File file = new File(resourcePath, ResourcesUtils.PUBLIC_FILES);
				if (file.exists()) {
	%>
	&nbsp;
	<a
		href="download_folder.jsp?path=<%=path + File.separator
								+ ResourcesUtils.PUBLIC_FILES%>">Download
		public files</a>
	<%
		}
	%>
	</p>
	<%
		}
			if (hasUserEditRights) {
	%>
	<p>
	<a href="resource.jsp?path=<%=path + File.separator + "_files" + editParameter%>">
		Private extra resources </a>
	<%
		File file = new File(resourcePath,
						ResourcesUtils.PUBLIC_FILES);
				if (file.exists()) {
	%>
	&nbsp;
	<a href="download_folder.jsp?path=<%=path + File.separator + ResourcesUtils.FILES%>">Download
		files</a>
	<%
		}
	%>
	</p>
	<%
			}
		}
		}
	%>
</body>
</html>